package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.response.sys.NewsCategoryResponse;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.NewsCategory;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.INewsCategoryService;
import com.mars.module.admin.request.NewsCategoryRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 新闻分类控制层
 *
 * @author mars
 * @date 2024-03-03
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "新闻分类接口管理",tags = "新闻分类接口管理")
@RequestMapping("/admin/newsCategory" )
public class NewsCategoryController {

    private final INewsCategoryService iNewsCategoryService;

    /**
     * 分页查询新闻分类列表
     */
    @ApiOperation(value = "分页查询新闻分类列表")
    @PostMapping("/pageList")
    public R<PageInfo<NewsCategory>> pageList(@RequestBody NewsCategoryRequest newsCategoryRequest) {
        return R.success(iNewsCategoryService.pageList(newsCategoryRequest));
    }

    /**
     * 获取菜单树
     *
     * @param queryDto 查询
     * @return R
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取菜单树")
    public R<List<NewsCategoryResponse>> list(@Validated @RequestBody NewsCategoryRequest queryDto) {
        return R.success(iNewsCategoryService.tree(queryDto));
    }

    /**
     * 获取新闻分类详细信息
     */
    @ApiOperation(value = "获取新闻分类详细信息")
    @GetMapping(value = "/query/{id}")
    public R<NewsCategory> detail(@PathVariable("id") Long id) {
        return R.success(iNewsCategoryService.getById(id));
    }

    /**
     * 新增新闻分类
     */
    @Log(title = "新增新闻分类", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增新闻分类")
    @PostMapping("/add")
    public R<Void> add(@RequestBody NewsCategoryRequest newsCategoryRequest) {
        iNewsCategoryService.add(newsCategoryRequest);
        return R.success();
    }

    /**
     * 修改新闻分类
     */
    @Log(title = "修改新闻分类", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改新闻分类")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody NewsCategoryRequest newsCategoryRequest) {
        iNewsCategoryService.update(newsCategoryRequest);
        return R.success();
    }

    /**
     * 删除新闻分类
     */
    @Log(title = "删除新闻分类", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除新闻分类")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iNewsCategoryService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出新闻分类
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody NewsCategoryRequest newsCategoryRequest) throws IOException {
        List<NewsCategory> list = iNewsCategoryService.list(newsCategoryRequest);
        ExcelUtils.exportExcel(list, NewsCategory.class, "新闻分类信息", response);
    }
}
